using System.Data;
using System.Linq;
using System.Dynamic;
using System.Collections.Generic;

namespace Ayatta.Common.Extension
{
    internal sealed class DynamicDataRow : DynamicObject
    {
        private readonly DataRow row;
        public DynamicDataRow(DataRow row) { this.row = row; }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = row[binder.Name];
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            row[binder.Name] = value;
            return true;
        }

        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return row.Table.Columns.Cast<DataColumn>().Select(dc => dc.ColumnName);
        }
    }
}